package com.zzptc.shop.servlet;

import com.zzptc.shop.dao.IDAO;
import com.zzptc.shop.dao.UserDao;
import com.zzptc.shop.vo.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "UserByIDServlet")
public class UserByIDServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      /*  request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //接受id
        String id=request.getParameter("id");
        String type=request.getParameter("type");
        IDAO dao=new UserDao();
        Users users=null;
        ////赋值 获取数据 方法一：从数据库中查找
        try {
            users=((UserDao) dao).getUsersByID(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        HttpSession se=request.getSession();
        //传递数据Users
        se.setAttribute("userinfo",users);
        //转发或跳转
        request.getRequestDispatcher("manage/user-modify.jsp").forward(request,response);
    }
    */
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //接受id
        String id=request.getParameter("id");
        String type=request.getParameter("type1");
        Users users=null;
        //赋值 获取数据 方法一：从数据库中查找 方法二：从SEssion中集合获取  遍历集合去找到该用户
        HttpSession se=request.getSession();
        List<Users> list=(List) se.getAttribute("allusers");
        for (Users s:list){
            if (s.getEu_user_id().equals(id)){
                users=s;
                break;
            }
        }
        se.setAttribute("userinfo",users);
        request.getRequestDispatcher("manage/user-modify.jsp").forward(request,response);
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
